"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : longblob数据类型
Case Name   : gs_probackup支持longblob类型的数据
Create At   : 2022.08.11
@zou_jialiang0501129488
Description :
    1、创建b库
    2、b库下查询dolphin插件
    3、建表并插入数据后执行gs_probackup进行备份后查询数据
    4、清理环境
Expect      :
    1、成功
    2、成功
    3、成功
    4、成功
History     :
"""

import os
import re
import unittest

from yat.test import Node
from yat.test import macro

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

sh_primary = CommonSH('PrimaryDbUser')


@unittest.skipIf(2 > sh_primary.get_node_num(), '非1主2备环境不执行')
class DatatypeTest01(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.common = Common()
        self.user_node = Node('PrimaryDbUser')
        self.sh_s1_user = CommonSH('Standby1DbUser')
        self.sh_s2_user = CommonSH('Standby2DbUser')
        self.constant = Constant()
        self.back_path = os.path.join(os.path.dirname(macro.DB_INSTANCE_PATH),
                                      'longblob_case0043_backup')
        self.log.info(sh_primary.get_db_cluster_status(param='detail'))

    def test_longblob(self):
        text = '-----step1:创建兼容b库数据库;expect:成功-----'
        self.log.info(text)
        sql_cmd = f"drop database if exists longblob_case43;" \
                  f"create database longblob_case43 " \
                  f"dbcompatibility ='B' encoding ='UTF8';"
        self.log.info(sql_cmd)
        sql_res = sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, sql_res,
                      '执行失败' + text)

        text = '-----step2:b库下查询dolphin插件;expect:自动加载成功,无需手动创建-----'
        self.log.info(text)
        sql_cmd = "select extname from pg_extension where extname ='dolphin';"
        self.log.info(sql_cmd)
        sql_res = sh_primary.execut_db_sql(sql_cmd, dbname='longblob_case43')
        self.log.info(sql_res)
        self.assertIn('extname', sql_res, '查询失败')
        self.assertIn('dolphin', sql_res, '执行失败' + text)

        text = '-----step3:建表并插入数据后进行备份后查询数据;expect:成功-----'
        self.log.info(text)
        cmd = f"rm -rf {self.back_path};mkdir -p {self.back_path};" \
              f"chmod 700 {self.back_path};" \
              f"ls {os.path.dirname(macro.DB_INSTANCE_PATH)} | " \
              f"grep 'longblob_case0043_backup';"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('longblob_case0043_backup', res, '文件夹创建失败')

        self.log.info('-----建表插数据-----')
        sql = f"drop table if exists t_longblob_043;" \
              f"create table t_longblob_043(field_name longblob);" \
              f"insert into t_longblob_043 values ('aaa'::blob);" \
              f"insert into t_longblob_043 values ('bbb'::blob);" \
              f"insert into t_longblob_043 values ('ccc'::blob);" \
              f"insert into t_longblob_043 values ('ddd'::blob);"
        self.log.info(sql)
        res = sh_primary.execut_db_sql(sql, dbname='longblob_case43')
        self.log.info(res)
        self.assertIn('CREATE TABLE', res, '建表失败')
        self.assertEqual(res.count('INSERT 0 1'), 4, '插入数据失败')
        select_sql = f"select * from t_longblob_043;"
        self.log.info(select_sql)
        check_msg = sh_primary.execut_db_sql(select_sql,
                                             dbname='longblob_case43')
        self.log.info(check_msg)

        self.log.info('-----执行gs_probackup init-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_probackup init -B {self.back_path}"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('successfully inited', res, 'init失败')

        self.log.info('-----执行gs_probackup add-instance-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_probackup add-instance -B {self.back_path} " \
              f"-D {macro.DB_INSTANCE_PATH} --instance=pro1"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('successfully inited', res, '建表失败')

        self.log.info('-----执行全量备份-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_probackup backup -B {self.back_path} -b full --stream " \
              f"--instance=pro1 -j 4 --skip-block-validation " \
              f"--no-validate -p {self.user_node.db_port} -d longblob_case43"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        re.search('Backup * completed', res)

        self.log.info('-----查看备份情况-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_probackup show -B {self.back_path} --instance=pro1"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        self.assertIn('DONE', res, '建表失败')

        sql = f"drop table if exists t_longblob_043;"
        self.log.info(sql)
        msg = sh_primary.execut_db_sql(sql, dbname='longblob_case43')
        self.log.info(msg)
        self.assertIn('DROP TABLE', msg, '表删除失败')

        self.log.info('-----stop原数据库-----')
        stop_res = sh_primary.stop_db_instance()
        self.assertIn('server stopped', stop_res, '建表失败')

        self.log.info('-----restore数据库-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_probackup restore -B {self.back_path} --instance=pro1 " \
              f"--incremental-mode=checksum -D {macro.DB_INSTANCE_PATH};"
        self.log.info(cmd)
        res = self.common.get_sh_result(self.user_node, cmd)
        re.search('Restore of backup * completed', res)

        self.log.info('-----启动数据库-----')
        start_res = sh_primary.start_db_instance()
        self.assertIn('server started', start_res, '启动失败')
        build_res1 = self.sh_s1_user.build_standby(mode='')
        self.assertIn('server started', build_res1, '备1build失败')
        build_res2 = self.sh_s2_user.build_standby(mode='')
        self.assertIn('server started', build_res2, '备2build失败')
        res = sh_primary.get_db_cluster_status(param='detail')
        self.log.info(res)
        self.assertIn('Normal' or 'Degraded', res, '重启失败')

        res = sh_primary.execut_db_sql(select_sql, dbname='longblob_case43')
        self.log.info(res)
        self.assertEqual(check_msg, res, '文件数据至表失败')

    def tearDown(self):
        self.log.info('-----step4:清理环境; expect:成功-----')
        cmd = f"source {macro.DB_ENV_PATH};" \
              f"gs_probackup del-instance -B {self.back_path} " \
              f"-D {macro.DB_INSTANCE_PATH} --instance=pro1"
        self.log.info(cmd)
        del_res = self.common.get_sh_result(self.user_node, cmd)
        check_cmd = f'rm -rf {self.back_path};' \
                    f'if [ -d {self.back_path} ]; ' \
                    f'then echo "exists"; else echo "not exists"; fi'
        self.log.info(check_cmd)
        res = self.common.get_sh_result(self.user_node, check_cmd)
        sql_cmd = f"drop database if exists longblob_case43;"
        self.log.info(sql_cmd)
        sql_res = sh_primary.execut_db_sql(sql_cmd, dbname='postgres')
        self.log.info(sql_res)
        self.assertIn('successfully deleted', del_res, '备份实例删除失败')
        self.assertEqual('not exists', res, '文件删除失败')
        self.assertIn('DROP DATABASE', sql_res, '数据库创建失败')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
